package com.dmap.admin.controller.backgroundv2;

import com.dmap.admin.bo.backgroundv2.DeviceTypeBo;
import com.dmap.admin.constants.AdminConstant;
import com.dmap.admin.controller.backgroundv2.param.DeviceTypeParam;
import com.dmap.admin.controller.backgroundv2.param.DeviceTypeSelectParam;
import com.dmap.admin.po.DeviceType;
import com.dmap.admin.service.backgroundv2.DeviceService;
import com.dmap.admin.service.backgroundv2.DeviceTypeService;
import com.dmap.admin.vo.backgroundv2.RecordNumAndDeviceTypeVo;
import com.dmap.base.constants.Constant;
import com.dmap.base.vo.BaseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * \* User: helei
 * \* Date: 27/05/18
 * \* Description:
 * \
 */
@Api(value = "个人数据服务系统-设备类型管理", description = "background/设备类型管理")
@Controller
@RequestMapping(value = "/background/pmpd-admin-server/v2/deviceType/")
public class DeviceTypeController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private DeviceTypeService deviceTypeService;
    @Resource
    private DeviceService deviceService;

    @ResponseBody
    @ApiOperation(value = "新增 deviceType",
            notes = "1. 必填： typeNumber, typeName; " +
                    "2. 返回id;")
    @RequestMapping(value = "insert", method = RequestMethod.POST)
    public BaseVo insert(@RequestBody DeviceTypeParam param) {
        BaseVo baseVo = new BaseVo();
        if (param == null || StringUtils.isBlank(param.getTypeNumber()) || StringUtils.isBlank(param.getTypeName())) {
            baseVo.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return baseVo;
        }
        boolean paramIsExist = deviceTypeService.validateNumberOrNameIsExist(param.getTypeNumber(), param.getTypeName());
        if (paramIsExist) {
            baseVo.setCode(AdminConstant.RESPONSE_DEVICE_TYPE_UNIQUE_ERROR_CODE);
            baseVo.setMsg(AdminConstant.RESPONSE_DEVICE_TYPE_UNIQUE_ERROR_MSG);
            return baseVo;
        }
        DeviceType insert = deviceTypeService.insertDeviceType(param);
        if (insert == null) {
            baseVo.setCode(Constant.RESPONSE_INSERT_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_INSERT_ERROR_MSG);
            return baseVo;
        }
        baseVo.setData(insert.getId().toString());
        return baseVo;
    }

    @ResponseBody
    @ApiOperation(value = "修改 deviceType",
            notes = "1. 必填： id, typeNumber, typeName; ")
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public BaseVo update(@RequestBody DeviceTypeParam param) {
        BaseVo baseVo = new BaseVo();
        if (param == null || StringUtils.isBlank(param.getId()) || !StringUtils.isNumeric(param.getId())
                || Long.valueOf(param.getId()).equals(0L)
                || StringUtils.isBlank(param.getTypeNumber()) || StringUtils.isBlank(param.getTypeName())) {
            baseVo.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return baseVo;
        }
        boolean success;
        try {
            success = deviceTypeService.updateDeviceType(param);
        } catch (RuntimeException e) {
            logger.error(e.getMessage());
            baseVo.setCode(AdminConstant.RESPONSE_DEVICE_TYPE_UNIQUE_ERROR_CODE);
            baseVo.setMsg(AdminConstant.RESPONSE_DEVICE_TYPE_UNIQUE_ERROR_MSG);
            return baseVo;
        }
        if (!success) {
            baseVo.setCode(Constant.RESPONSE_UPDATE_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_UPDATE_ERROR_MSG);
        }
        return baseVo;
    }

    @ResponseBody
    @ApiOperation(value = "根据id查询deviceType", notes = "1. 必填： id;")
    @RequestMapping(value = "select/selectById", method = RequestMethod.GET)
    public BaseVo selectById(@RequestParam String id) {
        BaseVo baseVo = new BaseVo();
        if (StringUtils.isBlank(id) || !StringUtils.isNumeric(id) || Long.valueOf(id).equals(0L)) {
            baseVo.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return baseVo;
        }
        DeviceType selectById = deviceTypeService.selectById(Long.valueOf(id));
        List<DeviceTypeBo> boList = new ArrayList<>();
        if (selectById != null) {
            DeviceTypeBo bo = new DeviceTypeBo(selectById);
            bo.setDeviceNum(deviceService.countByDeviceTypeId(selectById.getId()));
            boList.add(bo);
        }
        RecordNumAndDeviceTypeVo returnVo = new RecordNumAndDeviceTypeVo(1, boList);
        baseVo.setData(returnVo);
        return baseVo;
    }

    @ResponseBody
    @ApiOperation(value = "根据参数查询deviceType列表",
            notes = "1. 必填： pageIndex(>=1), pageSize(>=1); " +
                    "2. 返回deviceType列表; ")
    @RequestMapping(value = "select/selectByExample", method = RequestMethod.POST)
    public BaseVo selectByExample(@RequestBody DeviceTypeSelectParam param) {
        BaseVo baseVo = new BaseVo();
        if (param == null || param.getPageIndex() == null || param.getPageSize() == null
                || param.getPageIndex() < 1 || param.getPageSize() < 1) {
            baseVo.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return baseVo;
        }
        int recordNum = deviceTypeService.countByDeviceTypeSelectParam(param);
        List<DeviceTypeBo> boList = new ArrayList<>();
        if (recordNum > 0) {
            List<DeviceType> list = deviceTypeService.selectByDeviceTypeSelectParam(param);
            for (DeviceType deviceType : list) {
                if (deviceType != null) {
                    DeviceTypeBo bo = new DeviceTypeBo(deviceType);
                    bo.setDeviceNum(deviceService.countByDeviceTypeId(deviceType.getId()));
                    boList.add(bo);
                }
            }
        }
        RecordNumAndDeviceTypeVo returnVo = new RecordNumAndDeviceTypeVo(recordNum, boList);
        baseVo.setData(returnVo);
        return baseVo;
    }

}